import { request } from "../request";

/**
 * 既往病史信息接口
 */
export interface PastMedicalInfo {
  id?: string;
  idNumber?: string;
  name?: string;
  note?: string;
  createTime?: string;
  updateTime?: string;
  createBy?: string;
  updateBy?: string;
  valid?: number;
  [key: string]: any;
}

/**
 * 根据ID查询既往病史信息
 * @param id 记录ID
 * @returns Promise<PastMedicalInfo>
 */
export function getPastMedicalInfoById(id: string) {
  return request.get<PastMedicalInfo>(
    `/health_records/past_medical/pastMedicalInfo/queryById?id=${id}`,
    {},
    {
      useErrMsg: true,
    }
  );
}

/**
 * 编辑既往病史信息
 * @param data 既往病史信息数据
 * @returns Promise<PastMedicalInfo>
 */
export function editPastMedicalInfo(data: PastMedicalInfo) {
  console.log('=== API调用: editPastMedicalInfo ===')
  console.log('请求URL:', '/health_records/past_medical/pastMedicalInfo/edit')
  console.log('请求数据:', data)
  
  return request.post<PastMedicalInfo>(
    "/health_records/past_medical/pastMedicalInfo/edit",
    data,
    {
      useErrMsg: true,
    }
  ).then(response => {
    console.log('API响应:', response)
    return response
  }).catch(error => {
    console.error('API调用失败:', error)
    throw error
  });
}

/**
 * 获取既往病史信息列表
 * @param params 查询参数
 * @returns Promise<{list: PastMedicalInfo[], total: number}>
 */
export function getPastMedicalInfoList(params: { 
  pageNo?: number; 
  pageSize?: number; 
  idNumber?: string; 
  name?: string 
}) {
  const queryParams = new URLSearchParams();
  if (params.pageNo) queryParams.append("pageNo", params.pageNo.toString());
  if (params.pageSize) queryParams.append("pageSize", params.pageSize.toString());
  if (params.idNumber) queryParams.append("idNumber", params.idNumber);
  if (params.name) queryParams.append("name", params.name);

  return request.get<{ list: PastMedicalInfo[]; total: number }>(
    `/health_records/past_medical/pastMedicalInfo/list?${queryParams.toString()}`,
    {},
    {
      useErrMsg: true,
    }
  );
}

/**
 * 创建既往病史信息
 * @param data 既往病史信息数据
 * @returns Promise<PastMedicalInfo>
 */
export function createPastMedicalInfo(data: Omit<PastMedicalInfo, "id" | "createTime" | "updateTime">) {
  return request.post<PastMedicalInfo>(
    "/health_records/past_medical/pastMedicalInfo/add",
    data,
    {
      useErrMsg: true,
    }
  );
}

/**
 * 删除既往病史信息
 * @param id 记录ID
 * @returns Promise<void>
 */
export function deletePastMedicalInfo(id: string) {
  return request.delete(
    `/health_records/past_medical/pastMedicalInfo/delete?id=${id}`,
    {},
    {
      useErrMsg: true,
    }
  );
}
